package com.relax.netty.tcp.server;

import com.relax.netty.tcp.decode.MyDecode;
import com.relax.netty.tcp.encode.MyEncode;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class MyTcpServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();

        System.out.println("netty客户端  initChannel 执行:" + socketChannel.toString());
        //心跳
        pipeline.addLast("idleStateHandler", new IdleStateHandler(5, 0, 0, TimeUnit.SECONDS));
        pipeline.addLast("decode", new MyDecode());
        pipeline.addLast("encode", new MyEncode());
//        pipeline.addLast("decode", new FixedLengthFrameDecoder(6));
//         pipeline.addLast("decode", new LineBasedFrameDecoder(8));//换行分隔符
//         pipeline.addLast("decode", new LengthFieldBasedFrameDecoder(100,1,2,0,0));//换行分隔符

        pipeline.addLast(new MyTcpServerHandler());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }


}
